# coding: utf-8

import re
import datetime
import logging
from favshop.models import *
from django.utils import simplejson


def str2datetime(s):
    match_obj = re.compile(r'^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$')
    m = match_obj.match(s)
    ret_obj = None
    if m is not None:
        year, month, day, hour, minute, second = m.groups()
        ret_obj = datetime.datetime(int(year), int(month), int(day), 
            int(hour), int(minute), int(second))
    return ret_obj

def userinfo2outputdict(e):
    obj = None
    if e is not None:
        obj = {
            'nick': e.nick,
            'created': str(e.created),
            'last_visit': str(e.last_visit),
            'location_city': e.location_city,
            'location_state': e.location_state,
            'buyer_good_num': e.buyer_good_num,
            'buyer_level': e.buyer_level,
            'buyer_score': e.buyer_score,
            'buyer_total_num': e.buyer_total_num,
            'seller_good_num': e.seller_good_num,
            'seller_level': e.seller_level,
            'seller_score': e.seller_score,
            'seller_total_num':e.seller_total_num
        }
    return obj

def shopinfo2outputdict(e):
    obj = None
    if e is not None:
        obj = {
            'sid': e.sid,
            'cid': e.cid,
            'nick': e.nick,
            'title': e.title,
            'desc': e.desc,
            'bulletin': e.bulletin,
            'pic_path': e.pic_path, 
#            'create': str(e.create),
            'modified': str(e.modified),
        }
    return obj

def subscriptioninfo2outputdict(e):
    obj = None
    if e is not None:
        obj = {
            'nick': e.nick,
            'last_update_time': str(e.last_update_time),
        }
    return obj

def celist2outputlist(e):
    obj = None
    if e is not None:
        obj = [changeevent2outputdic(m) for m in e]
    return obj

def changeevent2outputdic(e):
    obj = None
    if e is not None:
        obj = {
            'summary': e.summary,
            'title': e.title,
            'content': e.content,
            'nick': e.nick,
            'timestamp': str(e.timestamp),
        }
        if e.goods_info is not None:
            obj['goods_iid'] = e.goods_info.iid
            obj['goods_title'] = e.goods_info.title
            obj['goods_type'] = e.goods_info.type
            obj['goods_cid'] = e.goods_info.cid
            obj['goods_pic_path'] = e.goods_info.pic_path
            obj['goods_delist_time'] = str(e.goods_info.delist_time)
            obj['goods_price'] = e.goods_info.price
            obj['goods_post_fee'] = e.goods_info.post_fee
    return obj

def ullist2outputlist(e):
    obj = None
    if e is not None:
        obj = [updatelog2outputdic(m) for m in e]
    return obj

def updatelog2outputdic(e):
    obj = None
    if e is not None:
        obj = {
            'nick': e.nick,
            'update_timestamp': str(e.update_timestamp),
            'log_msg': e.log_msg,
            'log_status': e.log_status,
        }
    return obj

def getcurrentupdating():
    entity = CurrentUpdatingInfo.get_by_key_name('favshop')
    if entity is None:
        entity = CurrentUpdatingInfo(key_name='favshop')
        entity.put()
    return entity
